/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easemob.chatuidemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.chatuidemo.adapter.PickGroupAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;

import net.mihome.app.R;

import java.util.List;

public class PickGroupsActivity extends BaseActivity {
	private ListView groupListView;
	protected List<EMGroup> grouplist;
	private PickGroupAdapter groupAdapter;

    private String forward_msg_id;
    protected boolean pauseOnScroll = false;
    protected boolean pauseOnFling = true;
    private ImageLoader imageLoader;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_groups);
        groupListView = (ListView)findViewById(R.id.list);
        findViewById(R.id.iv_new_contact).setVisibility(View.GONE);
        forward_msg_id = getIntent().getStringExtra("forward_msg_id");
        imageLoader= ImageLoader.getInstance();
        groupListView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
		grouplist =	EMGroupManager.getInstance().getAllGroups();
		groupAdapter = new PickGroupAdapter(this, 1, grouplist,imageLoader);
		groupListView.setAdapter(groupAdapter);
		groupListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {try {
                    ChatActivity.activityInstance.finish();
                        } catch (Exception e) {
                    }
					//进入圈子
					Intent intent = new Intent(PickGroupsActivity.this, ChatActivity.class);
					// it is group chat
					intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
                    intent.putExtra("forward_msg_id", forward_msg_id);
					intent.putExtra("groupId", groupAdapter.getItem(position - 1).getGroupId());
					startActivityForResult(intent, 0);
            }

		});
	}

	/**
	 * 返回
	 * @param view
	 */
	public void back(View view){
		finish();
	}
}
